package com.atguigu.mvc.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.FrameworkServlet;

import javax.servlet.ServletContext;

@Controller
public class TargetHandler {

    @Autowired
    private ServletContext servletContext;

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @RequestMapping("/send/request")
    public String sendRequest() {

        // 获取 ContextLoaderListener 创建的 IOC 容器对象
        String attrNameFromContext = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
        WebApplicationContext springIOCContainer = (WebApplicationContext) servletContext.getAttribute(attrNameFromContext);
        logger.debug(springIOCContainer.toString());

        // 获取 DispatcherServlet 创建的 IOC 容器对象
        String attrNameFromDispatch = FrameworkServlet.SERVLET_CONTEXT_PREFIX + "dispatcherServlet";
        WebApplicationContext springMVCIOCContainer = (WebApplicationContext) servletContext.getAttribute(attrNameFromDispatch);
        logger.debug(springMVCIOCContainer.toString());

        return "target";
    }

}
